package com.example.studydemo.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.graphics.withRotation
import androidx.core.graphics.withTranslation
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.example.studydemo.R
import kotlinx.coroutines.*

class DrawDemoView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) , LifecycleObserver {
    private val TAG = "MyView"
    private var mWidth = 0f
    private var mHeight = 0f
    private var mRadius = 0f
    private var mAngle = 10f

    private val solidLinePaint = Paint().apply {
        style = Paint.Style.STROKE
        strokeWidth = 5f
        color = ContextCompat.getColor(context, R.color.white)
    }
    private val vectorLinePaint = Paint().apply {
        style = Paint.Style.STROKE
        strokeWidth = 5f
        color = ContextCompat.getColor(context, R.color.purple_700)
    }

    private val textPaint = Paint().apply {
        textSize = 50f
        typeface = Typeface.DEFAULT_BOLD
        color = ContextCompat.getColor(context, R.color.white)
    }

    private val dashedPaint = Paint().apply {
        style = Paint.Style.STROKE
        pathEffect = DashPathEffect(floatArrayOf(10f, 10f), 0f)
        color = ContextCompat.getColor(context, R.color.purple_500)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w.toFloat()
        mHeight = h.toFloat()
        mRadius = if (w < h / 2) w / 2.toFloat() else h / 4.toFloat()
        mRadius -= 50f
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            drawAxises(this)
            drawLabel(this)
            drawDashedCircle(this)
            drawVector(this)
        }
    }

    private fun drawLabel(canvas: Canvas) {
        canvas.apply {
            drawRect(100f, 100f, 600f, 250f, solidLinePaint)
            drawText("指数函数与旋转矢量", 120f, 195f, textPaint)
        }
    }

    private fun drawAxises(canvas: Canvas) {
        /*java
        保存画布状态
        canvas.save()
        移动画布
        canvas.translate(mWidth/2,mHeight/2)
        绘制内容
        恢复画布
        canvas.restore()
        */
        //保存画布->移动画布到x,y->进行绘制->恢复画布
        canvas.withTranslation(mWidth / 2, mHeight / 2) {
            drawLine(-mWidth / 2, 0f, mWidth / 2, 0f, solidLinePaint)
            drawLine(0f, -mHeight / 2, 0f, mHeight / 2, solidLinePaint)
        }
        canvas.withTranslation(mWidth / 2, mHeight * 3 / 4) {
            drawLine(-mWidth / 2, 0f, mWidth / 2, 0f, solidLinePaint)
        }
    }



    private fun drawDashedCircle(canvas: Canvas) {
        canvas.withTranslation(mWidth / 2, mHeight / 4 * 3) {
            drawCircle(0f, 0f, mRadius, dashedPaint)
        }
    }

    private fun drawVector(canvas: Canvas) {
        canvas.withTranslation(mWidth / 2, mHeight / 4 * 3) {
            withRotation(mAngle) {
                drawLine(0f, 0f, mRadius, 0f, vectorLinePaint)
            }
        }
    }

    private var rotatingJob: Job? = null
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun startRotating(){
        rotatingJob = CoroutineScope(Dispatchers.Main).launch {
            while (true){
                delay(100)
                mAngle+=5f
                invalidate()
            }
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun stopRotating(){
        rotatingJob?.cancel()
    }
    private val fillPaint=Paint().apply {
        style=Paint.Style.STROKE
        strokeWidth=5f
        color=ContextCompat.getColor(context,R.color.white)
    }
}